package com.xiaoq.parking.repository.impl;

import java.util.List;

import com.xiaoq.coms.util.IdGeneratorUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.parking.entity.ParkingEntity;
import com.xiaoq.parking.repository.IParkingRepository;

@Repository
public class ParkingRepositoryImpl implements IParkingRepository {
	
	@Autowired
	private Datastore dataStore;

	@Override
	public ParkingEntity addParking(ParkingEntity parking) {
		dataStore.save(parking);
		return parking;
	}

	@Override
	public List<ParkingEntity> findParkings(Double latitude, Double longitude,String sitesId) {
		Query<ParkingEntity> parkingQuery = dataStore.createQuery(ParkingEntity.class)
		.field("loc").near(longitude, latitude).field("sitesId").equal(sitesId);
		return parkingQuery.asList();
	}

	@Override
	public void deleteParking(String id) {
		Query<ParkingEntity> infrastructQuery = dataStore.find(ParkingEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id);
		dataStore.delete(infrastructQuery);
	}

	@Override
	public List<ParkingEntity> findParkings(String cityCode) {
		Query<ParkingEntity> infrastructQuery = dataStore.find(ParkingEntity.class).field("cityCode").equal(cityCode);
		return infrastructQuery.asList();
	}

}
